package com.zmj.utils;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DBUtils {
   static {
       try {
           Class.forName("com.mysql.jdbc.Driver");
       } catch (ClassNotFoundException e) {
           e.printStackTrace();
       }
   }

   public static  Connection getConnection(){

       try {
         Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3307/testing?useUnicode=true&characterEncoding=utf-8&useSSL=true","root","zmj12345");
         return  connection;
       } catch (SQLException e) {
           e.printStackTrace();
       }
        return  null;
   }
    public static void colse(ResultSet rs, Connection cn,PreparedStatement ps){
        if (rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (cn!=null){
            try {
                cn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps!=null){
            try {
               ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void colse( Connection cn,PreparedStatement ps){
       colse(null,cn,ps);
    }


    //DMl语言的封装
    public  static  int excuteDMl(String sql,Object... params)throws  Exception {
        Connection cn = DBUtils.getConnection();
        PreparedStatement ps=cn.prepareStatement(sql);
        if (null!=params&&params.length>0) {
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
        }
        int num=ps.executeUpdate();

        return num;

    }
    //	查询多元素返回集合的封装

    public static <T> List<T> executeDQL(Class cls, String sql, Object... params) throws Exception {

        List<T> list = new ArrayList<T>();

        Connection cn = DBUtils.getConnection();

        PreparedStatement ps = cn.prepareStatement(sql);

        if (null != params && params.length > 0) {
            for (int i = 1; i <= params.length; i++) {
                ps.setObject(i, params[i - 1]);
            }
        }
        ResultSet rs = ps.executeQuery();
        ResultSetMetaData metaData = rs.getMetaData();
        int count = metaData.getColumnCount();

        T obj;

        while (rs.next()) {
            obj = (T) cls.newInstance();

            for (int i = 1; i <= count; i++) {

                String columnName = metaData.getColumnLabel(i);

                Object columnValue = rs.getObject(columnName);

                Field field = cls.getDeclaredField(columnName);

                if(columnValue != null){
                    field.setAccessible(true);
                    field.set(obj, columnValue);
                }
            }
            list.add(obj);
        }
        return list;
    }
    //	 查询单个元素的方法封装


    public static <T> T executeDQLByOne(Class cls, String sql, Object... params) throws Exception{

        List<T> list = executeDQL(cls,sql,params);

        if(null != list && list.size() >0){
            return list.get(0);
        }

        return null;

    }
}
